﻿using black_screen_mode_server.Models;
using black_screen_mode_server.Pages;
using black_screen_mode_server.Utils;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace black_screen_mode_server.ViewModel
{
    public class GroupSettingWindowViewModel:INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler? PropertyChanged;
        private ObservableCollection<GroupInfo> groupInfos = new ObservableCollection<GroupInfo>();
        public ICommand DeleteCommand { get; set; }

        public ICommand AddCommand { get; set; }

        public ObservableCollection<GroupInfo> GroupInfos
        {
            get
            {
                return groupInfos;
            }
            set
            {
                groupInfos = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(groupInfos)));
            }
        }

        public GroupSettingWindowViewModel()
        {
            refresh();
            DeleteCommand = new DelegateCommand(Delete, CanDelete);
            AddCommand = new DelegateCommand(add, CanAdd);
        }



        private void Delete(object obj)
        {
            GroupInfo info = obj as GroupInfo;
            if (info != null)
            {
              
                try
                {
                    string sql = $"delete from [group] where Id ={info.Id} ";
                    SqlHelper helper = new SqlHelper(ProjectTools.configDbConnString);
                    helper.ExecuteNonQuery(sql);
                }
                catch (Exception)
                {

                    Logger.Singleton.Error("删除分组信息失败！");
                }

                refresh();
            }
        }



        private bool CanDelete(object obj)
        {
            return true;
        }


        private void add(object obj)
        {
            try
            {
                string newName = Microsoft.VisualBasic.Interaction.InputBox("请输入新的分组名称：", "命名");

                // 检查输入是否为空字符串
                if (!string.IsNullOrWhiteSpace(newName))
                {
                    using (SqlConnection connection = new SqlConnection(ProjectTools.configDbConnString))
                    {
                        connection.Open();

                        string query = $"select Id from [group] where Name = '{newName}';";
                        SqlCommand queryCommand = new SqlCommand(query, connection);
                        var reader = queryCommand.ExecuteReader();

                        if (reader.HasRows)
                        {
                            MessageBox.Show("分组名称已存在！");
                            Logger.Singleton.Error("分组名称已存在！");
                            return;
                        }
                        reader.Close();
                        string insert = "insert into [group] values (@NewName);";
                        SqlCommand command = new SqlCommand(insert, connection);
                        command.Parameters.AddWithValue("@NewName", newName);
                        command.ExecuteNonQuery();
                    }
                }
            }
            catch (Exception)
            {

                MessageBox.Show("添加分组失败！");
            }
           
            refresh();
        }

        private bool CanAdd(object obj)
        {
            return true;
        }


        public void refresh()
        {
            string sql = $"select * from [group]";
            SqlHelper helper = new SqlHelper(ProjectTools.configDbConnString);
            DataTable dt = helper.GetByTable(sql);

            List<GroupInfo> infos = new List<GroupInfo>();
            try
            {
                foreach (DataRow dr in dt.Rows)
                {
                    GroupInfo info = new GroupInfo();
                    info.Id = Convert.ToInt32(dr["Id"]);
                    info.Name = dr["Name"].ToString();
                    infos.Add(info);
                }
            }
            catch (Exception)
            {

                Logger.Singleton.Error("获取分组信息失败！");
            }


            groupInfos.Clear();


            foreach (var item in infos)
            {
                groupInfos.Add(item);
            }

        }
    }
}
